package ink.yql.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import ink.yql.model.User;
import yql.myutil.MyDate;

import java.io.Serializable;
import java.util.Date;

public class MyRandom implements Serializable {
    public static User randomUser() {
        User user = User.builder().
                  uname("GT" + new MyDate().getTime() / 1000).
                  usex(Math.random() > 0.5 ? "男" : "女").
                  ubirthday(new MyDate(new Date(System.currentTimeMillis() - (long) Math.random() * 40 * 1000 * 60 * 60 * 24 * 365)).toString()).
                  usummary("个人简介是空白的").
                  uattest(0).
                  build();
        return user;
    }

    private static String randomCode() {
        String ret = "";
        String[] all = "0123456789".split("");
        for (int i = 0; i < 6; i++) {
            ret += all[(int) (Math.random() * 6)];
        }
        return ret;
    }
    public static String sendPhoneCode(String phone) {
        String code = randomCode();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                  "LTAI4Fkv8qrcoE9269MnLNuY",
                  "xC5KIGNqY53yawYKFmCLJfCIwGUO8S");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "17634298930");
        request.putQueryParameter("SignName", "小小勇者");
        request.putQueryParameter("TemplateCode", "SMS_203186809");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return code;
    }
}
